package thread;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * 生产者消费者模型
 */
public class MyBlockingQueue {
    public static void main(String[] args) {
        java.util.concurrent.BlockingQueue blockQueue = new ArrayBlockingQueue(5);
        //生产者
        //生产数据, 放在队列里面
        Thread producer = new Thread(()->{
            try {
                int num = 0;
                for (int i = 0; i < 100; i++) {
                    System.out.println("生产了数据:" + num);
                    num ++;
                    blockQueue.put(num);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        Thread consumer = new Thread(()->{
            try {
                //不停的从队列中取值
                while (true){
                    int num = (int) blockQueue.take();
                    System.out.println("消费数据:"+ num);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });
        producer.start();
        consumer.start();
    }
}
